<template>
  <div>
    <TopFilter ref="topFilter" :list="topFilterList" :cols="4" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>

    <FilterTable
      ref="table"
      :isSelectColumn="true"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onRowSelectChange="selectChange"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls">
        <el-button :disabled="isChoice" size="small" type="primary" icon="el-icon-plus" @click="release">发布</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>
      </template>
    </FilterTable>

    <Drawer :visible.sync="visible" :title="dialogTitle" :width="1100" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <CareTaskPanel :type="handleType" :recordVersion="recordVersion" :carePlanId="carePlanId" @changeHandle="changeHandle" @close="closeHandler" />
    </Drawer>
  </div>
</template>
<script>
import { getCareTaskList, deleteCareTaskList, releaseCareTask } from '@/api/customerRepair/visitRules/careTask2';
import { dictionary } from '@/utils/dictMixin';
import CareTaskPanel from './careTaskPanel';

export default {
  name: 'CareTask',
  components: {
    CareTaskPanel
  },
  mixins: [dictionary],
  data() {
    this.conf = { add: '新增', edit: '编辑', show: '明细' };
    return {
      topFilterList: this.createTopFilters(),
      columns: this.createTableColumns(),
      fetchApi: getCareTaskList,
      fetchParams: {},
      recordVersion: '',
      handleType: '',
      carePlanId: '',
      isChoice: true,
      selectedRows: [],
      dialogTitle: '',
      visible: false
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    createTopFilters() {
      return [
        {
          type: 'SELECT',
          label: '关爱类型',
          fieldName: 'careTypeCode',
          placeholder: '请选择...',
          itemList: this.createDictList('1572')
        },
        {
          type: 'INPUT',
          label: '关爱名称',
          fieldName: 'careName',
          placeholder: '请输入...'
        },
        {
          type: 'SELECT',
          label: '分配人员',
          fieldName: 'distribute',
          placeholder: '请输入...',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '是否启用',
          fieldName: 'whetherEnable',
          placeholder: '请选择...',
          itemList: this.createDictList('1004')
        },
        {
          type: 'RANGE_DATE',
          label: '创建日期',
          fieldName: 'createdAtStart|createdAtEnd',
          format: 'yyyy-MM-dd',
          placeholder: '创建日期'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          width: 140,
          render: (props, h) => {
            return (
              <div>
                <el-button size="mini" type="text" disabled={props.row.releaseTag == 10651002} onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" disabled={props.row.releaseTag == 10651002} onClick={() => this.deleteHandle(props.row)}>
                  删除
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" onClick={() => this.detailHandle(props.row)}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '计划编号',
          dataIndex: 'planNo',
          width: 125,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '关爱类型',
          dataIndex: 'careTypeCode',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.careTypeCode, '1572')}</div>;
          }
        },
        {
          title: '关爱名称',
          dataIndex: 'careName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '模板类型',
          dataIndex: 'careWay',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.careWay, '1587')}</div>;
          }
        },
        {
          title: '创建日期',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '基准日期',
          dataIndex: 'baseDateCode',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.baseDateCode, '1590')}</div>;
          }
        },
        {
          title: '距基准天数',
          dataIndex: 'baseDateDays',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '基准前后',
          dataIndex: 'baselineCode',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.baselineCode, '1589')}</div>;
          }
        },
        {
          title: '模板名称',
          dataIndex: 'templateName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '分配方式',
          dataIndex: 'distributeWay',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.distributeWay, '1586')}</div>;
          }
        },
        {
          title: '分配人员',
          dataIndex: 'distributeUserName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否启用',
          dataIndex: 'whetherEnable',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.whetherEnable, '1004')}</div>;
          }
        },
        {
          title: '执行周期',
          dataIndex: 'executeCycle',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.executeCycle, '1588')}</div>;
          }
        },
        {
          title: '发布状态',
          dataIndex: 'releaseTag',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.releaseTag, '1065')}</div>;
          }
        }
      ];
    },
    release() {
      let param = [];
      this.selectedRows.map(obj => {
        let data = { carePlanId: obj.carePlanId, recordVersion: obj.recordVersion };
        param.push(data);
      });
      releaseCareTask(param).then(res => {
        if (res.resultCode == 200) {
          this.$message.success('发布成功!');
          this.fetchParams = { ...this.fetchParams };
        } else {
          this.$message.error('发布失败!');
          return;
        }
      });
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    selectChange(rows) {
      // 选中时触发的函数
      this.selectedRows = rows;
      this.isChoice = this.selectedRows.length > 0 ? false : true;
    },
    showHandler() {
      this.dialogTitle = `${this.conf[this.handleType]}关爱计划`;
      this.visible = true;
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.recordVersion = row.recordVersion + '';
      this.carePlanId = row.carePlanId + '';
      this.showHandler();
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    changeHandle(val) {
      if (val.createdAtStart && val.createdAtStart != '') {
        val.createdAtStart = val.createdAtStart.substring(0, 10);
      }
      if (val.createdAtEnd && val.createdAtEnd != '') {
        val.createdAtEnd = val.createdAtEnd.substring(0, 10);
      }
      this.fetchParams = Object.assign({}, val);
    },
    deleteHandle(row) {
      this.$confirm('此操作将删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteCareTaskList({ carePlanId: row.carePlanId })
            .then(res => {
              if (res.resultCode == 200) {
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                });
                this.fetchParams = { ...this.fetchParams };
              }
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    },
    detailHandle(row) {
      this.handleType = 'show';
      this.carePlanId = row.carePlanId + '';
      this.showHandler();
    }
  }
};
</script>

<style lang="less"></style>
